//
//  GCRegistViewModel.m
//  CBHGroupCar
//
//  Created by mac on 2018/2/1.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYRegiserViewModel.h"
#import "Head.h"
@interface GYRegiserViewModel()
{
    //
    NSInteger leftSecond;
    NSTimer *timer;
}
@end
@implementation GYRegiserViewModel
#pragma mark - getter and setter
-(BOOL)canSendCode
{
    if ([self.validateCodeStatus isEqualToString:@"重新发送"] || [self.validateCodeStatus isEqualToString:@"获取验证码"]) {
        return YES;
    }else{
        return NO;
    }
}
#pragma mark - method

-(void)sendCode
{
    if ([WFFunctions WFStrIsMobileNumber:self.registPhone])
    {
        @WeakSelf;
        [FTIndicator showSuccessWithMessage:@"验证码已发送到您的手机"];
        [selfp timerBegin];
//        NSMutableDictionary *para = [[NSMutableDictionary alloc]init];
//        [para setObject:self.registPhone forKey:@"mobile"];
//        [para setObject:self.registerType forKey:@"bz"];
//        NSString *inter = @"code";
//        [[InterfaceManager shareInterface]requetInterface:inter withParameter:para handler:^(NSDictionary *info,InterfaceStatusModel *infoModel) {
//            [selfp timerBegin];
//        }];
    }else{
        [FTIndicator showErrorWithMessage:@"手机号格式错误"];
    }

}
-(void)timerBegin
{
    timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerSet:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)requestgetCode
{

}
-(void)requestRegister
{
    NSMutableDictionary *para = [[NSMutableDictionary alloc]init];
    [para setObject:[NSString stringWithFormat:@"%@",self.registPhone] forKey:@"mobile"];
    [para setObject:[NSString stringWithFormat:@"%@",self.validateCode] forKey:@"code"];
    [para setObject:[NSString stringWithFormat:@"%@",self.password] forKey:[self.registerType isEqualToString:@"1"]?@"psw":@"pwd"];

    if (![WFFunctions WFStrCheckEmpty:self.inviteInfo])
    {
        [para setObject:self.inviteInfo forKey:@"rcode"];
    }
    
    NSString *inter;
    if ([self.registerType isEqualToString:@"1"])
    {
        GYLoginViewModel* model=[GYLoginViewModel shareInterface];
        inter=model.roleType==1?@"regist":model.roleType==2?@"storageRegister":@"logisticsRegisterl";
    }else
    {
        inter=@"findBackPwd";
         //[self.registerType isEqualToString:@"1"]?@"regist":@"findBackPwd"
    }
    [[InterfaceManager shareInterface]requetInterface:inter withParameter:para handler:^(NSDictionary *info,InterfaceStatusModel *infoModel) {
        [self setSuccess:YES];
        if ([self.registerType isEqualToString:@"1"])
        {
            [self requsetLogin];
        }
        [FTIndicator showSuccessWithMessage:infoModel.message];
    }];
}


-(void)requsetLogin
{
    NSMutableDictionary* paraDic=[[NSMutableDictionary alloc] init];
    paraDic[@"uname"]=self.registPhone;
    paraDic[@"psw"]=self.password;
    
    GYLoginViewModel* model=[GYLoginViewModel shareInterface];
    
    [[InterfaceManager shareInterface] requetInterface:model.roleType==1?@"login":model.roleType==2?@"storageLogin":@"logisticsLogin" withParameter:paraDic handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [FTIndicator dismissProgress];
        NSDictionary *data = info[@"data"];
        GYLoginInfoModel* model=[[GYLoginInfoModel alloc] init];
        
        if ([[GYLoginViewModel shareInterface] roleType]==1)
        {
            model = [GYLoginInfoModel yy_modelWithJSON:data];
        }
        if ([[GYLoginViewModel shareInterface] roleType]==2)
        {
            model.loginmobile=[data objectForKey:@"mobile"];
            model.access_token=[data objectForKey:@"access_token"];
        }
        if ([[GYLoginViewModel shareInterface] roleType]==3)
        {
            model.loginmobile=[data objectForKey:@"mobile"];
            model.access_token=[data objectForKey:@"access_token"];
        }
        // GYLoginInfoModel *model = [GYLoginInfoModel yy_modelWithJSON:data];
        [[InterfaceManager shareInterface].httpManager.requestSerializer setValue:model.access_token forHTTPHeaderField:@"Authorization"];
        [model saveToDB];
        [[NSUserDefaults standardUserDefaults]setObject:model.loginmobile forKey:kLoginUsername];
         [[NSUserDefaults standardUserDefaults]setObject:self.password forKey:kLoginPsw];
        
        //paraDic[@"psw"]=[[NSUserDefaults standardUserDefaults]objectForKey:kLoginPsw];
        [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithInteger:[[GYLoginViewModel shareInterface] roleType]] forKey:kUserIdentity];
        [[GYLoginViewModel shareInterface] setLoginstatus:YES];
        
        
        
        if ([[GYLoginViewModel shareInterface] roleType]==1)
        {
            [[GYLoginViewModel shareInterface] setLoginInfoModel:model];
            [[GYUserViewModel shareInterface]requestUserInfo];
        }
        if ([[GYLoginViewModel shareInterface] roleType]==2)
        {
            GYStorageUserModel* storageUsermodel=[GYStorageUserModel yy_modelWithJSON:data];
            [[GYStorageUserViewModel shareInterface] setUserModel:storageUsermodel];
            [[GYStorageUserViewModel shareInterface] requestUserShopBaseInfo];
        }
        if ([[GYLoginViewModel shareInterface] roleType]==3)
        {
            GYStorageUserModel* storageUsermodel=[GYStorageUserModel yy_modelWithJSON:data];
            [[GYStorageUserViewModel shareInterface] setUserModel:storageUsermodel];
            [[GYStorageUserViewModel shareInterface] requestUserShopBaseInfo];
        }
        
    }];
}

-(void)timerSet:(NSTimer*)sender//定时器
{
    if(1 == leftSecond)
    {
        [sender invalidate];
        self.validateCodeStatus = @"重新发送";
        leftSecond = 60;
        [sender invalidate];
        sender = nil;
    }
    else
    {
        leftSecond--;
        self.validateCodeStatus = [NSString stringWithFormat:@"%ld秒",leftSecond];
    }
}
-(void)invalidateTimer
{
    [timer invalidate];
    timer = nil;
    leftSecond = 60;
    self.validateCodeStatus = @"获取验证码";
}
#pragma mark - life
-(instancetype)init
{
    if (self = [super init]) {
        leftSecond = 60;
        self.validateCodeStatus = @"获取验证码";
        self.success=NO;
    }
    return self;
}
@end

